package cool.tianyou.blue.bird;


import android.app.Notification;
import android.os.Bundle;
import android.service.notification.StatusBarNotification;

/**
 * 符合条件的通知过滤
 */
public class Filter {

    /**
     * 包名
     */
    private String pkgNameRegex;
    /**
     * 标题
     */
    private String titleRegex;
    /**
     * 文本
     */
    private String textRegex;

    public Filter() {
    }

    public boolean match(StatusBarNotification notification) {
        return false;
    }

    public Filter(String pkgNameRegex, String titleRegex, String textRegex) {
        this.pkgNameRegex = pkgNameRegex;
        this.titleRegex = titleRegex;
        this.textRegex = textRegex;
    }

    public static Filter createWithPlanText(String pkgNameRegex, String titleRegex, String textRegex) {
        Filter filter = new Filter();
        if (titleRegex != null) {
            filter.titleRegex = titleRegex.replaceAll("([.()\\[\\]\\{\\}*+?:<>])", "\\\\$1");
        }
        if (textRegex != null) {
            filter.textRegex = textRegex.replaceAll("([.()\\[\\]\\{\\}*+?:<>])", "\\\\$1");
        }
        filter.pkgNameRegex = pkgNameRegex.replaceAll("([.()\\[\\]\\{\\}*+?:<>])", "\\\\$1");
        return filter;
    }


    /**
     * 如果包名，标题，内容和 过滤器匹配返回true;
     *
     * @param pkgName 包名
     * @param title   标题
     * @param text    内容
     * @return
     */
    public boolean match(String pkgName, String title, String text) {
        if (pkgNameRegex != null && pkgName != null && pkgName.matches(this.pkgNameRegex)) {
            if (titleRegex == null) {
                return true;
            }
            if (title != null && title.matches(this.titleRegex)) {
                if (textRegex == null) {
                    return true;
                }
                return text != null && text.matches(this.textRegex);
            }
            return false;
        }
        return false;
    }


}
